<?php

namespace App\Repository\Repositories;

use App\Repository\Repositories\Interfaces\UserCollectionRepository;
use App\Repository\Models\UserCollection;

/**
 * Class UserCollectionRepositoryEloquent.
 *
 * @package namespace App\Repository\Repositories;
 */
class UserCollectionRepositoryEloquent extends BaseRepository implements UserCollectionRepository
{
    /**
     * 搜索
     * @var array
     */
    protected $fieldSearchable = [
        'type',
        'user_id',
        'shop_id',
        'is_invalid',
        'created_at' => 'between',
    ];

    /**
     * 多字段联合模糊查询,前端查询字段名keyword
     * @var array
     */
    public $fieldFuzzySearch = [
        'type'
    ];

    /**
     * 可关联查询的字段
     */
    public $withAllow = [
        'product',
        'sku'
    ];

    /**
     * Specify Model class name
     *
     * @return string
     */
    public function model()
    {
        return UserCollection::class;
    }
    /**
     * Specify Model class name
     *
     * @return string
     */
    public function checkCollection($sku_id,$shop_id,$user_id=null)
    {
        $this->applyCriteria();
        $this->applyScope();
        if ($user_id!=null){
            // 检测商品是否收藏
            $goods = UserCollection::query()->where(['sku_id' => intval($sku_id),'type'=>1,'user_id'=>$user_id,'deleted_at'=>null])->get()->toArray();

            $checkGoods = count($goods)>0?true:false;

            // 检测店铺是否被收藏
            $getShop   = UserCollection::query()->where(['shop_id' => intval($shop_id),'type'=>2,'user_id'=>$user_id,'deleted_at'=>null])->get()->toArray();
            $checkShop = count($getShop)>0?true:false;

            return ['checkGoods'=>$checkGoods,'checkShop'=>$checkShop];
        }else{
            return ['checkGoods'=>false,'checkShop'=>false];
        }
    }
    /**
     * Specify Model class name
     *
     * @return string
     */
    public function checkMultiCollection($sku_ids,$user_id=null)
    {
        $this->applyCriteria();
        $this->applyScope();
        $sku_ids = explode(',',$sku_ids);

        $checkGoods = [];
        if ($user_id!=null){
            foreach ($sku_ids as $key=>$value){
                // 检测商品是否收藏
                $goods = UserCollection::query()->where(['sku_id' => intval($value),'type'=>1,'user_id'=>$user_id,'deleted_at'=>null])->get()->toArray();
                if (count($goods)>0){
                    $checkGoods[] = $value;
                }
            }
        }
            
        return $checkGoods;
        
    }
}
